﻿using B2BPublisher.App;
using B2BPublisher.App.Manager;
using B2BPublisher.App.Windows;
using B2BPublisher.BbsSite;
using B2BPublisher.Sv;
using B2BPublisher.UI;
using B2BPublisher.UI.Wpf;
using Publisher;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace MediaPublisher
{
    internal static class Program
    {
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);


        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                WebDriverInit webDriverInit = new WebDriverInit();
                webDriverInit.InitDll();
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex.Message, ex.StackTrace);
            }
            try
            {
                ServicePointManager.Expect100Continue = false;
                ScreenDpi.GetDpi();
            }
            catch
            {
            }
            SvHelper.IsMediaPublisher = true;

            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //处理UI线程异常
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //处理非UI线程异常
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Publisher.EncryptMap.EnableEncrypt = true;


            try
            {
                RegHelper reg = new RegHelper();
                reg.DisableFips();
                OemInfoForBbs.AgentId = OemInfo.Instance.AgentId;
                SvHelper.AgentId = OemInfo.Instance.AgentId;

                var icon = Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location);
                OemInfo.Instance.IconIcon = icon;

                InitSvHelperAgentId();
            }
            catch { }

            try
            {
                SoftwareVersionIniter versionIniter = new SoftwareVersionIniter();
                var version = FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
                versionIniter.Init(version.FileVersion);
            }
            catch { }

            try
            {
                ProxySettingIniter proxyIniter = new ProxySettingIniter();
                proxyIniter.StartInitThread();

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                var dialog = new frmMainWpf();
                ElementHost.EnableModelessKeyboardInterop(dialog);
                dialog.ShowDialog();
            }
            catch(Exception ex)
            {
                //todo 处理启动失败的问题
                Logger.WriteLog(ex.Message, ex.StackTrace);
            }

            BbsManager.Instance.LogoutAll();
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        private static void InitSvHelperAgentId()
        {
#if DEBUG
            SvHelper.AgentId = 16;
#endif
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            new Publisher.Logger("err.txt").Write("Exception", "CurrentDomain_UnhandledException", e.ExceptionObject.ToString());
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            new Publisher.Logger("err.txt").Write("Exception", "Application_ThreadException", e.Exception.StackTrace.ToString());
        }
    }
}
